package com.hsoft.zhxy.app.xtgl_02_01.service.impl;


import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import com.hsoft.zhxy.app.xtgl_02_01.mapper.JcqxGnzyMapper;
import com.hsoft.zhxy.app.common.model.entity.JcqxGnzy;
import com.hsoft.zhxy.app.xtgl_02_01.model.query.GnzyQuery;
import com.hsoft.zhxy.app.xtgl_02_01.model.vo.GnzyVO;
import com.hsoft.zhxy.app.xtgl_02_01.service.JcqxGnzyService;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.hsoft.zhxy.common.base.BaseService;
import com.hsoft.zhxy.common.result.ResultBody;
import com.hsoft.zhxy.common.util.UUIDUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * All rights Reserved, Designed By 哈尔滨工业大学软件工程股份有限公司
 *
 * @Title: 功能资源 Service层接口实现类
 * @Package
 * @Description:
 * @author: system
 * @date: 2020-10-26
 */
@Log4j2
@Service
public class JcqxGnzyServiceImpl extends BaseService implements JcqxGnzyService {

    @Autowired
    private JcqxGnzyMapper jcqxGnzyMapper;


    /**
     * 查询单条数据
     *
     * @param jcqxGnzy
     * @return
     */
    public JSONObject findOne(JcqxGnzy jcqxGnzy) {
        JcqxGnzy rs = jcqxGnzyMapper.selectOne(new QueryWrapper<JcqxGnzy>().setEntity(jcqxGnzy));
        return ResultBody.setData(rs);
    }


    /**
     * 查询列表数据
     *
     * @return
     */
    public JSONObject findList() {
        List<GnzyVO> gzyList = jcqxGnzyMapper.selectGzyList();//获取全部根节点
        List<GnzyVO> zzyList = jcqxGnzyMapper.selectZzyList();//获取全部子节点
        JSONArray gzyArray = JSONArray.parseArray(JSONArray.toJSONString(gzyList));//转换格式
        JSONArray zzyArray = JSONArray.parseArray(JSONArray.toJSONString(zzyList));//转换格式
        JSONArray returnArray = new JSONArray();//返回的数据
        for (int i = 0; i < gzyArray.size(); i++) {
            JSONObject gzyJson = gzyArray.getJSONObject(i);
            this.getZzy(gzyJson, zzyArray);
            returnArray.add(gzyJson);
        }

        return ResultBody.setData(returnArray);
    }

    /**
     * 递归获取根目录的子资源
     * @param gzyJson
     * @param zzyArray
     */
    private void getZzy(JSONObject gzyJson, JSONArray zzyArray) {
        boolean hasChildren = false;
        JSONArray returnArray = new JSONArray();
        for (int i = 0; i < zzyArray.size(); i++) {
            JSONObject zzyJson = zzyArray.getJSONObject(i);

            if (gzyJson.getString("gnzyid").equals(zzyJson.getString("fcdid"))) {
                hasChildren = true;
                this.getZzy(zzyJson, zzyArray);
                returnArray.add(zzyJson);
            } else {
                continue;
            }
        }

        if (hasChildren) {
            gzyJson.put("children", returnArray);
        }
    }


    /**
     * 查询分页数据
     *
     * @param jcqxGnzy
     * @param page
     * @param pageSize
     * @return
     */
    public JSONObject findListByPage(JcqxGnzy jcqxGnzy, long page, long pageSize) {

        Page<JcqxGnzy> pageObj = new Page<JcqxGnzy>(page, pageSize);
        IPage<JcqxGnzy> result = jcqxGnzyMapper.selectPage(pageObj, new QueryWrapper<JcqxGnzy>().setEntity(jcqxGnzy));

        return ResultBody.setData(result.getTotal(), result.getRecords());
    }


    /**
     * 新增
     *
     * @param jcqxGnzy
     * @return
     */
    public JSONObject add(JcqxGnzy jcqxGnzy) {
        jcqxGnzy.setGnzyid(UUIDUtils.getUUID());
        jcqxGnzy.setCjsj(LocalDateTime.now());
        jcqxGnzy.setCjr(getYhid());
        jcqxGnzy.setBgsj(LocalDateTime.now());
        jcqxGnzy.setBgr(getYhid());
        return ResultBody.setData(jcqxGnzyMapper.insert(jcqxGnzy));
    }


    /**
     * 修改
     *
     * @param jcqxGnzy
     * @return
     */
    public JSONObject update(JcqxGnzy jcqxGnzy) {
        jcqxGnzy.setBgsj(LocalDateTime.now());
        jcqxGnzy.setBgr(getYhid());
        return ResultBody.setData(jcqxGnzyMapper.updateById(jcqxGnzy));
    }


    /**
     * 删除
     *
     * @param jcqxGnzy
     * @return
     */
    public JSONObject delete(JcqxGnzy jcqxGnzy) {
        return ResultBody.setData(jcqxGnzyMapper.delete(new QueryWrapper<JcqxGnzy>().setEntity(jcqxGnzy)));
    }




}
